import { createApp } from 'vue'
import router from './router'
import App from './App.vue'
import axios from 'axios'
import VueAxios from 'vue-axios'

import '~/assets/css/index.css'
import '~/assets/fonts/iconfont.css'

axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'
axios.interceptors.request.use(config => {
  const token = sessionStorage.getItem('token') || ''
  config.headers!.Authorization = token
  return config
})
axios.interceptors.response.use(response => {
  if (response.data.meta.status !== 200) {
    ElMessage({
      message: '请求调用失败 ' + response.data.meta.msg || '',
      type: 'error'
    })
    router.push('/login')
  }
  return response.data
})

const app = createApp(App)
app.use(router)
app.use(VueAxios, axios)
app.provide('axios', app.config.globalProperties.axios)
app.mount('#app')
